package com.jd.risk.bankorder.util.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 程序中处理文件可使用的帮助类
 * 
 * @author Liu Even
 */
public class FileHelper {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(FileHelper.class);

    /**
     * 获取xml输出格式 getXmlOutputFormat
     * 
     * @return the defined xml out put format
     */
    public static OutputFormat getXmlOutputFormat() {
        OutputFormat format = new OutputFormat("    ", true);
        format.setLineSeparator(System.getProperty("line.separator"));
        //utf-8 是默认的encoding，也许可以不用设置？
        format.setEncoding("utf-8");
        return format;
    }

    /**
     * 读取xml
     * 
     * @param is 
     * @return
     * @throws IOException 
     * @throws DocumentException
     */
    public static Document getXmlDocument(InputStream is) throws IOException  {
        SAXReader reader = new SAXReader();
        Document document = null;
		try {
			document = reader.read(is);
			
		} catch (DocumentException e) {
			LOGGER.error("读取xml：{}", e);
		}finally{
		     if (is != null) {
		    	 is.close();
	            }
		}
		return document;
    }

    /**
     * 读取xml
     * 
     * @param file
     * @return
     * @throws DocumentException
     */
    public static Document getXmlDocument(File file) throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(file);
        return document;
    }

    /**
     * 读取xlsx
     * 
     * @param is
     * @return
     * @throws IOException
     */
    public static XSSFWorkbook getWorkBook2007(InputStream is) throws IOException{
        XSSFWorkbook workbook = null;
		try {
			workbook = new XSSFWorkbook(is);
		} catch (IOException e) {
			LOGGER.error("读取xlsx：{}", e);
		}finally{
		     if (is != null) {
		    	 is.close();
	            }
		}
        return workbook;
    }

    /**
     * 读取
     * 
     * @param file
     * @return
     * @throws IOException
     */
    public static XSSFWorkbook getWorkBook2007(File file) throws IOException {
        FileInputStream is = null;
        XSSFWorkbook workbook = null;
        try {
			is = new FileInputStream(file);
			workbook = new XSSFWorkbook(is);
		} catch (FileNotFoundException e) {
			LOGGER.error("读取xlsx：{}", e);
		}finally{
		     if (is != null) {
		    	 is.close();
	            }
		}
        return workbook;
    }

    /**
     * 读取xls
     * 
     * @param is
     * @return
     * @throws IOException
     */
    public static HSSFWorkbook getWorkBook2003(InputStream is) throws IOException{
        HSSFWorkbook workbook = null;
        try {
			workbook = new HSSFWorkbook(is);
		} catch (IOException e) {
			LOGGER.error("读取xls：{}", e);
		}finally{
		     if (is != null) {
		    	 is.close();
	            }
		}
        return workbook;
    }

    /**
     * 读取xls〉
     * 
     * @param file
     * @return
     * @throws IOException
     */
    public static HSSFWorkbook getWorkBook2003(File file) throws IOException{
        FileInputStream is = null;
        HSSFWorkbook workbook = null;
        try {
			is = new FileInputStream(file);
			workbook = new HSSFWorkbook(is);
		} catch (FileNotFoundException e) {
			LOGGER.error("读取xls：{}", e);
		}finally{
		     if (is != null) {
		    	 is.close();
	            }
		}
        return workbook;
    }

}